package com.demo.study.thread.JUC;

import java.util.Hashtable;

/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2021/1/28 17:15
 */
public class MyConcurrentHashMap<K, V> {

    private Hashtable<K, V>[] segments;

    public MyConcurrentHashMap() {
        segments = new Hashtable[16];
    }

    public void put(K k, V v) {
        //第一次计算index  计算key存放在那个hashtablezhong 
        int index = k.hashCode() & (segments.length - 1);
        Hashtable<K, V> segment = segments[index];
        if (segment == null) {
            segment = new Hashtable<>();
        }
        segment.put(k, v);
        segments[index] = segment;
    }

    public V get(K k) {
        int index = k.hashCode() & (segments.length - 1);
        Hashtable<K, V> segment = segments[index];
        if (segment != null) {
            return segment.get(k);
        }
        return null;
    }

    public static void main(String[] args) {
        MyConcurrentHashMap myConcurrentHashMap = new MyConcurrentHashMap();
        for (int i = 0; i < 10; i++) {
            myConcurrentHashMap.put(i+"",i+"");
        }
        for (int i = 0; i <10 ; i++) {
            System.out.println(myConcurrentHashMap.get(i+""));
        }
    }
}
